/********* GameControlPlugin.m Cordova Plugin Implementation *******/

#import <Cordova/CDV.h>
#import "VideoPrizeClawViewController.h"
#import "MMGVenderSocialManager.h"
#import "UIDevice+MMGUUID.h"

@interface GameControlPlugin : CDVPlugin {
    // Member variables go here.
}

- (void)getUDID:(CDVInvokedUrlCommand*)command;
- (void)appVersion:(CDVInvokedUrlCommand*)command;
- (void)init:(CDVInvokedUrlCommand*)command;
- (void)play:(CDVInvokedUrlCommand*)command;
- (void)openUrl:(CDVInvokedUrlCommand*)command;
@end

@implementation GameControlPlugin

- (void)getUDID:(CDVInvokedUrlCommand*)command {
    NSString* UDID= [UIDevice UUID];
    NSString* userPhoneName = [[UIDevice currentDevice] name];
    [self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:[NSString stringWithFormat:@"%@|%@",UDID,userPhoneName]] callbackId:command.callbackId];
}

-(void) appVersion:(CDVInvokedUrlCommand *)command{
    //return app version
    NSString* version= kMMG_SYS_APP_VERSION;//[NSString stringWithFormat:@"%@(%@)",kMMG_SYS_APP_VERSION,kMMG_SYS_APP_BUILD_VERSION];
    DLog(@"app version %@",version);
    [self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:version] callbackId:command.callbackId];
}

-(void) init:(CDVInvokedUrlCommand *)command{

    //iOS 这里啥也不干
}
-(void) play:(CDVInvokedUrlCommand *)command{
    DLog(@"control plugin play ......");
    
//    //视频RMTP地址，测试写死139.199.191.99
//    NSString* rtmpAddress = @"139.199.191.99";//[command.arguments objectAtIndex:0];
//    //商品（公仔）ID******
//    NSString* productId = [command.arguments objectAtIndex:1];
//    //商品（公仔）价钱，一局币额
//    NSString* price = [command.arguments objectAtIndex:2];
//    //用户登录授权CODE
//    NSString* userAuth = [command.arguments objectAtIndex:3];
//    //控制服务器地址
//    NSString* controlServerAddress = @"139.199.191.99";
//    //测试设备ID，以后为通过传productId上服务器，然后在服务器转换成deviceNO,目前暂时还未实现，所以先这样。
//    NSString* deviceNo = @"39d760f68c154f8fbc5cfda8be175a27";
    [[MMGGameSYSSettings sharedMMGGameSYSSettings] mmg_gameSystemSettings:command.arguments];
    
    dispatch_async(dispatch_get_main_queue(), ^{
        //后续使用 运行时 组件化 //target/action
        VideoPrizeClawViewController *vpcViewController = [[VideoPrizeClawViewController alloc] init];
        vpcViewController.commandDelegate = self.commandDelegate;
        vpcViewController.command = command;
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vpcViewController];
        
        [self.viewController presentViewController:nav animated:YES completion:nil];
    });
    
    
//    __block CDVPluginResult* pluginResult;
    //操作异常回调：echo为异常的消息文字内容
    //    pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:echo];
    //    [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
    
    //操作正常：echo："WIN" -> 中奖，"LOSE" ->不中奖
    //    pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:echo];
    //    [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
    
    
    
}
-(void) openUrl:(CDVInvokedUrlCommand *)command{
    NSString* urlStr = [command.arguments objectAtIndex:0];
    NSURL *url= [NSURL URLWithString:urlStr];
    if([[UIApplication sharedApplication] canOpenURL:url])
    {
        [[UIApplication sharedApplication]openURL:url];
    }
    else
    {
        DLog(@"can not open");
    }
}
@end
